package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.PhotoViewActivity;
import com.example.city_home_app.Activity.UserInfoActivity;
import com.example.city_home_app.Bean.Down_Msg;
import com.example.city_home_app.Bean.Down_MsgCencter;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.MyCircleImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_Msg extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_Msg> list;
    private int userid;

    public Adapter_Msg(Context context) {
        this.context = context;
    }

    public void setList(List<Down_Msg> list) {
        this.list = list;
        this.userid = BaseActivity.getUserinfo().getUserid();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_message, parent, false);
        Adapter_Msg.ViewHolder viewHolder = new Adapter_Msg.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_Msg bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_Msg.ViewHolder viewHolder = (Adapter_Msg.ViewHolder) holder;

        //判断是图片还是文字
        if(bean.getMsg_type()==1){
            //文字
            viewHolder.llMessageImage.setVisibility(View.GONE);
            viewHolder.llMessageNormal.setVisibility(View.VISIBLE);
                //判断发送者
                if(bean.getSend_user()==userid){
                    //用户发送文字
                    viewHolder.llMsgNormalSend.setVisibility(View.GONE);
                    viewHolder.llMsgNormalYour.setVisibility(View.VISIBLE);
                    viewHolder.tvMsgNormalYourtime.setText(bean.getMsg_time());
                    viewHolder.tvMsgNormalYourcontent.setText(bean.getMsg_content());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getSender_image())
                            .into(viewHolder.imgMsgNormalYourhead);
                }else{
                    //对方发送文字
                    viewHolder.llMsgNormalSend.setVisibility(View.VISIBLE);
                    viewHolder.llMsgNormalYour.setVisibility(View.GONE);
                    viewHolder.tvMsgNormalSendtime.setText(bean.getMsg_time());
                    viewHolder.tvMsgNormalSendcontent.setText(bean.getMsg_content());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getSender_image())
                            .into(viewHolder.imgMsgNormalSendhead);
                }
        }else{
            //图片
            viewHolder.llMessageImage.setVisibility(View.VISIBLE);
            viewHolder.llMessageNormal.setVisibility(View.GONE);
                //判断发送者
                if(bean.getSend_user()==userid){
                    //用户发送图片
                    viewHolder.llMsgImageSend.setVisibility(View.GONE);
                    viewHolder.llMsgImageYour.setVisibility(View.VISIBLE);
                    viewHolder.tvMsgImageYourtime.setText(bean.getMsg_time());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getSender_image())
                            .into(viewHolder.imgMsgImageYourhead);
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getMsg_content())
                            .into(viewHolder.imgMsgImageYourimage);
                }else{
                    //对方发送图片
                    viewHolder.llMsgImageSend.setVisibility(View.VISIBLE);
                    viewHolder.llMsgImageYour.setVisibility(View.GONE);
                    viewHolder.tvMsgImageSendtime.setText(bean.getMsg_time());
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getSender_image())
                            .into(viewHolder.imgMsgImageSendhead);
                    Picasso.with(context)
                            .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getMsg_content())
                            .into(viewHolder.imgMsgImageSendimage);
                }
        }

        viewHolder.imgMsgImageSendimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> imgurls = new ArrayList<>();
                imgurls.add(bean.getMsg_content());
                Intent intent = new Intent(context, PhotoViewActivity.class);
                intent.putStringArrayListExtra("urls",imgurls);
                intent.putExtra("currentPosition", 0);
                context.startActivity(intent);
            }
        });

        viewHolder.imgMsgImageYourimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> imgurls = new ArrayList<>();
                imgurls.add(bean.getMsg_content());
                Intent intent = new Intent(context, PhotoViewActivity.class);
                intent.putStringArrayListExtra("urls",imgurls);
                intent.putExtra("currentPosition", 0);
                context.startActivity(intent);
            }
        });

        viewHolder.imgMsgImageSendhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getSend_user());
                context.startActivity(intent);
            }
        });

        viewHolder.imgMsgImageYourhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getSend_user());
                context.startActivity(intent);
            }
        });

        viewHolder.imgMsgNormalSendhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getSend_user());
                context.startActivity(intent);
            }
        });

        viewHolder.imgMsgNormalYourhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("id",bean.getSend_user());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_msg_normal_sendhead)
        MyCircleImageView imgMsgNormalSendhead;
        @BindView(R.id.tv_msg_normal_sendcontent)
        TextView tvMsgNormalSendcontent;
        @BindView(R.id.tv_msg_normal_sendtime)
        TextView tvMsgNormalSendtime;
        @BindView(R.id.ll_msg_normal_send)
        LinearLayout llMsgNormalSend;
        @BindView(R.id.tv_msg_normal_yourcontent)
        TextView tvMsgNormalYourcontent;
        @BindView(R.id.tv_msg_normal_yourtime)
        TextView tvMsgNormalYourtime;
        @BindView(R.id.img_msg_normal_yourhead)
        MyCircleImageView imgMsgNormalYourhead;
        @BindView(R.id.ll_msg_normal_your)
        LinearLayout llMsgNormalYour;
        @BindView(R.id.ll_message_normal)
        LinearLayout llMessageNormal;
        @BindView(R.id.img_msg_image_sendhead)
        MyCircleImageView imgMsgImageSendhead;
        @BindView(R.id.img_msg_image_sendimage)
        ImageView imgMsgImageSendimage;
        @BindView(R.id.tv_msg_image_sendtime)
        TextView tvMsgImageSendtime;
        @BindView(R.id.ll_msg_image_send)
        LinearLayout llMsgImageSend;
        @BindView(R.id.img_msg_image_yourimage)
        ImageView imgMsgImageYourimage;
        @BindView(R.id.tv_msg_image_yourtime)
        TextView tvMsgImageYourtime;
        @BindView(R.id.img_msg_image_yourhead)
        MyCircleImageView imgMsgImageYourhead;
        @BindView(R.id.ll_msg_image_your)
        LinearLayout llMsgImageYour;
        @BindView(R.id.ll_message_image)
        LinearLayout llMessageImage;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
